<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/11/24
+--------------------------------------------------------------
 * Time: 21:44
+--------------------------------------------------------------
 * 基类,所有action都继承此类,创建所有控制器公用的方法
+--------------------------------------------------------------
 */
class BaseAction extends Action{

    const Teacher_id  = 'liuyong';//特殊的用户id
    protected static $action = 0; //导航从左至又的顺序值,用于前端页面控制显示当前样式
    /**
     * 初始化信息,类似构造函数
     */
    public function _initialize(){
//        $_SESSION['user_id'] = "liuyong";
        $this->assign('action',self::$action);
        $this->assign('menus',C('menus'));//菜单
        $this->assign('session',$_SESSION);
//        dump($_SESSION);exit;
        if(!empty($_SESSION['user_id'])){//登录后获取未读邮件个数
            $mail = D('Mail')->getMailCount();
            $this->assign('mail_count',$mail);
        }
    }

    /**
     * @param $id
     * @return mixed|string
     * 过滤输入不合法的题目id 防止sql注入
     */
    public function filterId($id){
        if(empty($id)){
            return '';
        }
        $id = str_replace("，",",",$id);
        $ids = explode(",",$id);
        $temp = array();
        foreach($ids as $k=>$v){
            $temp[] = intval($v);
        }
        $id = implode(',',$temp);
        return $id;
    }

    /**
     * @param $user_id
     * @return int|string
     * 过滤用户输入的user_id防止sql注入
     */
    public function filterUserid($user_id){

        $id = trim($user_id);
        if(empty($id)){
            return '';
        }
        if (is_numeric($id)) {
            if($id != intval($id)){
                return doubleval($id); //学号超过十位数
            }
            return intval($id);
        }
        if (is_string($id)) {
            if (self::Teacher_id === strtolower($id)) {
                return self::Teacher_id;
            } else {
                return '';
            }
        }
    }
}